{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Design Assist: Transmission Line with Load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "include(\"TLines.jl\")\n",
    "import TLines: μ0, ϵ0\n",
    "import InspectDR\n",
    "using NumericIO\n",
    "\n",
    "#Convenience aliases:\n",
    "SI(x) = formatted(x, :SI, ndigits=3)\n",
    "\n",
    "#When MIME\"image/svg+xml\" is disabled, Jupyter eventually requests PNG inline graphics:\n",
    "#(SVG outputs do not render well in notebooks for some reason...)\n",
    "InspectDR.defaults.rendersvg = false\n",
    ";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setup: Interact, Reactive, and Signals\n",
    "(Also create initial plot object)\n",
    "\n",
    "**NOTE:** Zref=50Ω when computing reflection coefficient, Γ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Interact, Reactive\n",
    "\n",
    "ltypelist = [(\"Series RLC\" => :series), (\"Shunt RLC\" => :shunt)]\n",
    "ltypemap = Dict{Symbol, String}(v=>s for (s,v) in ltypelist)\n",
    "ltype = Signal(Symbol, :series) #Load type\n",
    "fmin = Signal(Float64, 1e7) #Minimum plot frequency\n",
    "fmax = Signal(Float64, 10e9) #Maximum plot frequency\n",
    "R = Signal(Float64, 40) #Resistive load (Ω)\n",
    "L = Signal(Float64, 20e-9) #Inductive load (H)\n",
    "C = Signal(Float64, 0.2e-12) #Capacitive load (F)\n",
    "ZC = Signal(Float64, 75) #Characteristic impedance of line (Ω)\n",
    "α = Signal(Float64, 0.2) #Line attenuation (dB/m) - Nepers/m?\n",
    "μ = Signal(Float64, μ0) #Permeability for line (H/m)\n",
    "ϵ = Signal(Float64, ϵ0) #Permittivity for line (F/m)\n",
    "ℓ = Signal(Float64, 0.1) #Line length (m)\n",
    "pobj = TLines.newplot() #Create initial plot object\n",
    ";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Interact/Reactive Control: Plot limits\n",
    "\n",
    "\n",
    "## TODO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#txt_fmax = textbox(value(fmax), label=\"max freq\", signal=fmax) #Will not display - notebook hangs??\n",
    "#display(txt_fmin) Will hang session using this method, for some reason\n",
    "txt_fmin = textbox(value(fmin), label=\"min freq\") #This one does not cause hangups\n",
    "fmin = signal(txt_fmin) #Hack to get fmax/txt_fmax working\n",
    "txt_fmax = textbox(value(fmax), label=\"max freq\")\n",
    "fmax = signal(txt_fmax)\n",
    "map(display, [txt_fmin, txt_fmax])\n",
    ";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Interact/Reactive Control: Inline Plots\n",
    "Use sliders/text boxes to change line parameters.\n",
    "\n",
    "## TODO\n",
    " \n",
    " - Control either RLGC or ZC, α, μ & ϵ line parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "tgl_ltype = togglebuttons(ltypelist, value_label=ltypemap[value(ltype)], signal=ltype)\n",
    "display(tgl_ltype)\n",
    "\n",
    "#RLC sliders:\n",
    "sld_R = slider(-3:.1:6, value=log10(value(R)), label=\"R-load: log(Ω)\")\n",
    "sld_L = slider(-15:.1:-3, value=log10(value(L)), label=\"L-load: log(H)\")\n",
    "sld_C = slider(-18:.1:-3, value=log10(value(C)), label=\"C-load: log(F)\")\n",
    "map(display, [sld_R, sld_L, sld_C])\n",
    "\n",
    "#Line attenuation slider:\n",
    "sld_α = slider(0:.001:1, value=value(α), label=\"α-DC line atten (dB/m)\", signal=α)\n",
    "display(sld_α)\n",
    "\n",
    "#Line length slider:\n",
    "sld_ℓ = slider(0:1e-6:1, value=value(ℓ), label=\"ℓ-line length (m)\", signal=ℓ)\n",
    "display(sld_ℓ)\n",
    "\n",
    "#Characteristic impedance slider:\n",
    "sld_ZC = slider(1:1.0:377, value=value(ZC), label=\"ZC (Ω)\", signal=ZC)\n",
    "display(sld_ZC)\n",
    "\n",
    "calc_R = map(logR->push!(R, 10.0^logR), signal(sld_R))\n",
    "calc_L = map(logL->push!(L, 10.0^logL), signal(sld_L))\n",
    "calc_C = map(logC->push!(C, 10.0^logC), signal(sld_C))\n",
    "\n",
    "display_params = map(R, L, C, ℓ) do _R, _L, _C, _ℓ\n",
    "    return HTML(\n",
    "        \"R=\" * SI(_R) * \"Ω\" *\n",
    "        \", L=\" * SI(_L) * \"H\" *\n",
    "        \", C=\" * SI(_C) * \"F\" *\n",
    "        \", ℓ=\" * SI(_ℓ) * \"m\"\n",
    "    )\n",
    "end\n",
    "display(display_params)\n",
    "\n",
    "#Widget-controlled plot:\n",
    "updater_plot = map(fmin, fmax, ltype, R, L, C, ℓ, ZC, α) do _fmin, _fmax, _ltype, _R, _L, _C, _ℓ, _ZC, _α\n",
    "    TLines.update(pobj, _fmin, _fmax, _ltype, _R, _L, _C, _ℓ, _ZC, _α)\n",
    "    pobj\n",
    "end\n",
    "display(updater_plot)\n",
    ";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Interact/Reactive Control: Inspect/Gtk GUI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtkgui = display(InspectDR.GtkDisplay(), pobj) #Display plot in Gtk GUI.\n",
    "\n",
    "#Refresh GUI when plot changes:\n",
    "updater_gtkgui = map((p)->InspectDR.refresh(gtkgui), updater_plot)\n",
    "\n",
    "#Re-display GUI controls, for convenience:\n",
    "#map(display, widgetlist)\n",
    ";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo complete!"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
